<?php
namespace LSYS\Area;
use LSYS\Area;

class CN{
	public static $china_code="156";
	protected static $_area;
	public static function area(Area $area=null){
		if ($area==null){
			if (self::$_area==null){
				self::$_area=Area::instance();
			}
			return self::$_area;
		}
		self::$_area=$area;
	}
	/**
	 * 取得省
	 * @param unknown $city
	 * @return Ambigous <NULL, multitype:>
	 */
	protected static function _get_area($code=null){
		$p=Area::instance()->childs($code);
		return $p;
	}
	/**
	 * 简化地区
	 * @param string $area
	 * @return string
	 */
	protected static function _simplify_area($area){
		$rep=array("自治县",'自治州',"自治区","特别行政区","区","省","市","县","维吾尔");
		$_area=$area;
		foreach ($rep as $v){
			$_area=str_replace($v, "", $_area);
		}
		if(strpos($_area, '族')!==false){
			$mz=array('蒙古族','回族','藏族','维吾尔族','苗族','彝族','壮族','布依族','朝鲜族','满族','侗族','瑶族','白族','土家族','哈尼族','哈萨克族','傣族','黎族','傈僳族','佤族','畲族','高山族','拉祜族','水族','东乡族','纳西族','景颇族','柯尔克孜族','土族','达斡尔族','仫佬族','羌族','布朗族','撒拉族','毛南族','仡佬族','锡伯族','阿昌族','普米族','塔吉克族','怒族','乌孜别克族','俄罗斯族','鄂温克族','德昂族','保安族','裕固族','京族','塔塔尔族','独龙族','鄂伦春族','赫哲族','门巴族','珞巴族','基诺族');
			foreach ($mz as $v){
				$_area=str_replace($v, "", $_area);
				$_area=str_replace($v, "", $_area);
				$_area=str_replace($v, "", $_area);
			}
		}
		if(mb_strlen($_area)<=1)
			return $area;
			return $_area;
	}
	/**
	 * 解析地址
	 * @param string $area
	 * @return string
	 */
	public static function discover_china_area($area_str,$join=' '){
		$province=array();
		$city=array();
		$district=array();
		if(strpos($area_str,"国")!==false&&strpos($area_str,"中国")===false){
			return array(false,null,null,array());
		}
		$_str=$area_str;
		$pro=self::_get_area(self::$china_code);
		foreach ($pro as $vs){
			$v=self::_simplify_area($vs['name']);
			$p=strpos($area_str, $v);
			if($p!==false){
				
				$_p=strpos($area_str, $vs['name']);
				if ($_p!==false)$p=$_p+strlen($vs['name']);
				else $p=$p+strlen($v);
				
				$_str=substr($area_str, $p);
				$province=$vs;
				$citys=self::_get_area($vs['code']);
				if(!is_array($citys))break;
				foreach ($citys as $kk=>$vvs){
					$vv=self::_simplify_area($vvs['name']);
					$p=strpos($area_str, $vv);
					if($p!==false){
						
						$_p=strpos($area_str, $vvs['name']);
						if ($_p!==false)$p=$_p+strlen($vvs['name']);
						else $p=$p+strlen($vv);
						
						$_str=substr($area_str, $p);
						$city=$vvs;
						$diss=self::_get_area($vvs['code']);
						if(!is_array($diss)) break;
						foreach ($diss as $kkk=>$vvvs){
							$vvv=self::_simplify_area($vvvs['name']);
							$p=strpos($area_str, $vvv);
							if($p!==false){
								$_p=strpos($area_str, $vvvs['name']);
								if ($_p!==false)$p=$_p+strlen($vvvs['name']);
								else $p=$p+strlen($vvv);
								$_str=substr($area_str,$p);
								$district=$vvvs;
								break 3;
							}
						}
						break 2;
					}
				}
				break;
			}
		}
		$code=false;
		if (isset($province['code']))$code=$province['code'];
		if (isset($city['code']))$code=$city['code'];
		if (isset($district['code']))$code=$district['code'];
		$data=array(
			$province,$city,$district
		);
		return array($code,self::area()->join($data, $join),trim($_str),$data);
	}
}